package top.cyuw.subjectsystem.controller;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.cyuw.subjectsystem.dto.PagingParamDto;
import top.cyuw.subjectsystem.dto.user.UserCreateRequest;
import top.cyuw.subjectsystem.dto.user.UserEditRequest;
import top.cyuw.subjectsystem.dto.user.UserImportDto;
import top.cyuw.subjectsystem.dto.user.UserLoginRequest;
import top.cyuw.subjectsystem.service.UserService;
import top.cyuw.subjectsystem.util.ExcelUtils;
import top.cyuw.subjectsystem.vo.ImportResultVo;
import top.cyuw.subjectsystem.vo.PagingVo;
import top.cyuw.subjectsystem.vo.ResponseVo;
import top.cyuw.subjectsystem.vo.user.UserDetailVo;
import top.cyuw.subjectsystem.vo.user.UserVo;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    ResponseVo<UserVo> login(@RequestBody UserLoginRequest request) {
        return ResponseVo.success(userService.login(request));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    ResponseVo<Boolean> create(@RequestBody UserCreateRequest request) {
        return ResponseVo.success(userService.create(request));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    ResponseVo<Boolean> delete(@RequestParam Integer id) {
        return ResponseVo.success(userService.delete(id));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    ResponseVo<Boolean> edit(@RequestBody UserEditRequest request) {
        return ResponseVo.success(userService.edit(request));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    ResponseVo<PagingVo<UserDetailVo>> search(@RequestBody PagingParamDto<String> request) {
        return ResponseVo.success(userService.search(request));
    }

    @RequestMapping(value = "/import", method = RequestMethod.POST)
    ResponseVo<ImportResultVo> importFromExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<UserImportDto> list = ExcelUtils.readExcel(file.getInputStream(), UserImportDto.class);
        ImportResultVo vo = userService.importFromExcel(list);
        return ResponseVo.success(vo);
    }

    @RequestMapping(value = "/template", method = RequestMethod.GET)
    void template(HttpServletResponse response) throws Exception {
        String fileName = "template-user-import.xlsx";
        InputStream inputStream = FileUtils.openInputStream(new File(fileName));
        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
        response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(inputStream.available()));
        IOUtils.copy(inputStream, response.getOutputStream(), 24);
    }

}
